package org.chnxi.study.concurrent.locks;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLoclDemo01 {

    private static int nums = 100;

    private static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {

        for (int i=0; i<1; i++){
            new Thread(()->{
                while (true){

                    try {
                        TimeUnit.MILLISECONDS.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    lock.lock();
                    try{
                        if(nums > 0){
                            nums = nums -1;
                            System.out.println(Thread.currentThread().getName()+":"+nums);
                        }else{
                            System.out.println(Thread.currentThread().getName()+":售罄");
                            return;
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }finally {
                        lock.unlock();
                    }
                }
            }, "Thread-"+i).start();
        }

    }

}
